﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using RedemptoristRetreatCenter.DatabaseUtility;

namespace RedemptoristRetreatCenter
{
    /// <summary>
    /// View Volunteer Class allows admin users to view a list of active volunteers and their volunteer tasks 
    /// </summary>
    public partial class ViewVolunteer : System.Web.UI.Page
    {
        /// <summary>
        /// Upon loading the ViewVolunteer page, create and populate the table to be tied to the gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable volunteerTable = new DataTable();
            DataRow tableRow = null;
            //Boolean isAdmin;
            UserAccountUtility userUtil = new UserAccountUtility();
            VolunteerUtility volUtil = new VolunteerUtility();
            List<UserAccountInfo> userInfoList = userUtil.getVolunteerList();

            // -- creating the volunteer table --
            volunteerTable.Columns.Add(new DataColumn("First Name", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Last Name", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Home Phone", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Email", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Bulk Mailing", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Carpentry", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Bush Removal", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Computer Maintenance", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Electrical", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Plant Care", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Data Entry", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("HVAC", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Leaf Raking", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("General Office", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Painting", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Pruning", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Library", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Weeding", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Word Processing", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Refrigeration", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Roofing and Gutters", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Road Repair", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Small Engine Repair", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Window Washing", typeof(string)));
            volunteerTable.Columns.Add(new DataColumn("Plumbing", typeof(string)));
            //volunteerTable.Columns.Add(new DataColumn("Activities", typeof(string)));

            // -- populate the volunteer table --
            foreach (UserAccountInfo userInfo in userInfoList)
            {
                List<int> taskIDs = new List<int>();
                String taskShortDescs = null;

                tableRow = volunteerTable.NewRow();
                tableRow["First Name"] = userInfo.firstName;
                tableRow["Last Name"] = userInfo.lastName;
                tableRow["Home Phone"] = userInfo.homePhone;
                tableRow["Email"] = userInfo.emailAddress;

                taskIDs = volUtil.getVolTaskIdsForUser(userInfo.userId);

                // -- add the volunteer tasks --
                foreach (int taskId in taskIDs)
                {
                    taskShortDescs = volUtil.getVolTaskShortDescByTaskID(taskId);

                    if ((taskShortDescs.Equals("Carpentry")) && (!tableRow["Carpentry"].Equals("x")))
                        tableRow["Carpentry"] = "x";
                    else if ((taskShortDescs.Equals("Bush Removal")) && (!tableRow["Bush Removal"].Equals("x")))
                        tableRow["Bush Removal"] = "x";
                    else if ((taskShortDescs.Equals("Computer Maintenance")) && (!tableRow["Computer Maintenance"].Equals("x")))
                        tableRow["Computer Maintenance"] = "x";
                    else if ((taskShortDescs.Equals("Electrical")) && (!tableRow["Electrical"].Equals("x")))
                        tableRow["Electrical"] = "x";
                    else if ((taskShortDescs.Equals("Plant Care")) && (!tableRow["Plant Care"].Equals("x")))
                        tableRow["Plant Care"] = "x";
                    else if ((taskShortDescs.Equals("Data Entry")) && (!tableRow["Data Entry"].Equals("x")))
                        tableRow["Data Entry"] = "x";
                    else if ((taskShortDescs.Equals("HVAC")) && (!tableRow["HVAC"].Equals("x")))
                        tableRow["HVAC"] = "x";
                    else if ((taskShortDescs.Equals("Leaf Raking")) && (!tableRow["Leaf Raking"].Equals("x")))
                        tableRow["Leaf Raking"] = "x";
                    else if ((taskShortDescs.Equals("General Office")) && (!tableRow["General Office"].Equals("x")))
                        tableRow["General Office"] = "x";
                    else if ((taskShortDescs.Equals("Painting")) && (!tableRow["Painting"].Equals("x")))
                        tableRow["Painting"] = "x";
                    else if ((taskShortDescs.Equals("Pruning")) && (!tableRow["Pruning"].Equals("x")))
                        tableRow["Pruning"] = "x";
                    else if ((taskShortDescs.Equals("Library")) && (!tableRow["Library"].Equals("x")))
                        tableRow["Library"] = "x";
                    else if ((taskShortDescs.Equals("Weeding")) && (!tableRow["Weeding"].Equals("x")))
                        tableRow["Weeding"] = "x";
                    else if ((taskShortDescs.Equals("Word Processing")) && (!tableRow["Word Processing"].Equals("x")))
                        tableRow["Word Processing"] = "x";
                    else if ((taskShortDescs.Equals("Refrigerator")) && (!tableRow["Refrigeration"].Equals("x")))
                        tableRow["Refrigeration"] = "x";
                    else if ((taskShortDescs.Equals("Roofing and Gutters")) && (!tableRow["Roofing and Gutters"].Equals("x")))
                        tableRow["Roofing and Gutters"] = "x";
                    else if ((taskShortDescs.Equals("Road Repair")) && (!tableRow["Road Repair"].Equals("x")))
                        tableRow["Road Repair"] = "x";
                    else if ((taskShortDescs.Equals("Small Engine Repair")) && (!tableRow["Small Engine Repair"].Equals("x")))
                        tableRow["Small Engine Repair"] = "x";
                    else if ((taskShortDescs.Equals("Window Washing")) && (!tableRow["Window Washing"].Equals("x")))
                        tableRow["Window Washing"] = "x";
                    else if ((taskShortDescs.Equals("Plumbing")) && (!tableRow["Plumbing"].Equals("x")))
                        tableRow["Plumbing"] = "x";

                    //if(taskShortDescs != null) 
                    //    taskShortDescs = taskShortDescs + ", " + volUtil.getVolTaskShortDescByTaskID(taskId);
                    //else
                    //    taskShortDescs = volUtil.getVolTaskShortDescByTaskID(taskId);
                }

                //tableRow["Activities"] = taskShortDescs;

                volunteerTable.Rows.Add(tableRow);
            }

            ViewState["CurrentTable"] = volunteerTable;

            VolGridView.DataSource = volunteerTable;
            VolGridView.DataBind();

            //if (Session["isAdmin"] == null)
            //    isAdmin = false;
            //else
            //    isAdmin = (Boolean)Session["isAdmin"];

            //if (isAdmin)
            //    addVolunteer.Visible = true;
            //else
            //    addVolunteer.Visible = false;
        }

        /// <summary>
        /// Upon clicking the addVolunteer button, sends user to the the volunteer sign up page 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addVolunteer_Click(object sender, EventArgs e)
        {
            Response.Redirect("Volunteer.aspx");
        }
    }
}